<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="工艺编号" prop="processNumber">
        <el-input v-model="formData.processNumber" placeholder="请输入工艺编号" />
      </el-form-item>
      <el-form-item label="工艺名称" prop="processName">
        <el-input v-model="formData.processName" placeholder="请输入工艺名称" />
      </el-form-item>
      <el-form-item label="工艺单位" prop="processUnit">
        <el-select v-model="formData.processUnit" placeholder="请选择工艺单位">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.VALUATION_UNIT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品" prop="productionType">
        <el-select v-model="formData.productionType" placeholder="请选择产品">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CARDBOARD_PRODUCTION_PRODUCT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="是否外协" prop="isOutsource">
        <el-radio-group v-model="formData.isOutsource">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="外协用料规格" prop="outsourceMaterialSpec">
        <el-input v-model="formData.outsourceMaterialSpec" placeholder="请输入外协用料规格" />
      </el-form-item>
      <el-form-item label="生产数计算方式" prop="productionQuantityCalculateType">
        <el-select v-model="formData.productionQuantityCalculateType" placeholder="请选择生产数计算方式">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PRODUCTION_QUANTITY_CALCULATE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认用料序号" prop="defaultMaterialNo">
        <el-input v-model="formData.defaultMaterialNo" placeholder="请输入默认用料序号" />
      </el-form-item>
      <el-form-item label="科目" prop="subject">
        <el-input v-model="formData.subject" placeholder="请输入科目" />
      </el-form-item>
      <el-form-item label="MES必需手动结单" prop="mesManualComplete">
        <el-radio-group v-model="formData.mesManualComplete">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认半成品入库" prop="semiProductStorage">
        <el-radio-group v-model="formData.semiProductStorage">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="MES加载设计文件" prop="loadDesignDocuments">
        <el-radio-group v-model="formData.loadDesignDocuments">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="工艺类别" prop="processType">
        <el-input v-model="formData.processType" placeholder="请输入工艺类别" />
      </el-form-item>
      <el-form-item label="工艺状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择工艺状态">
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PROCESS_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="MES上机文件不存在时提示" prop="mesNoFileAlert">
        <el-radio-group v-model="formData.mesNoFileAlert">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否分单" prop="isSeparateOrder">
        <el-radio-group v-model="formData.isSeparateOrder">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认基础单价" prop="defaultUnitPrice">
        <el-input v-model="formData.defaultUnitPrice" placeholder="默认基础单价" />
      </el-form-item>
      <el-form-item label="上传文件" prop="uploadFile">
        <el-radio-group v-model="formData.uploadFile">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否按分单排产" prop="separateOrderScheduing">
        <el-radio-group v-model="formData.separateOrderScheduing">
          <el-radio
            v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="备注" name="baseProcessNameRemark">
        <BaseProcessNameRemarkForm ref="baseProcessNameRemarkFormRef" :process-id="formData.id" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
import { BaseProcessNameApi, BaseProcessNameVO } from '@/api/erp/baseprocessname'
import BaseProcessNameRemarkForm from './components/BaseProcessNameRemarkForm.vue'

/** ERP基础资料-生产工艺 表单 */
defineOptions({ name: 'BaseProcessNameForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  processNumber: undefined,
  processName: undefined,
  processUnit: undefined,
  productionType: undefined,
  remark: undefined,
  isOutsource: undefined,
  outsourceMaterialSpec: undefined,
  productionQuantityCalculateType: undefined,
  defaultMaterialNo: undefined,
  subject: undefined,
  mesManualComplete: undefined,
  semiProductStorage: undefined,
  loadDesignDocuments: undefined,
  processType: undefined,
  status: undefined,
  mesNoFileAlert: undefined,
  isSeparateOrder: undefined,
  defaultUnitPrice: undefined,
  uploadFile: undefined,
  separateOrderScheduing: undefined
})
const formRules = reactive({
  processNumber: [{ required: true, message: '工艺编号不能为空', trigger: 'blur' }],
  processName: [{ required: true, message: '工艺名称不能为空', trigger: 'blur' }],
  processUnit: [{ required: true, message: '工艺单位不能为空', trigger: 'change' }],
  productionType: [{ required: true, message: '产品不能为空', trigger: 'change' }],
  isOutsource: [{ required: true, message: '是否外协不能为空', trigger: 'blur' }],
  mesManualComplete: [{ required: true, message: 'MES必需手动结单不能为空', trigger: 'blur' }],
  semiProductStorage: [{ required: true, message: '默认半成品入库不能为空', trigger: 'blur' }],
  loadDesignDocuments: [{ required: true, message: 'MES加载设计文件不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '工艺状态不能为空', trigger: 'change' }],
  mesNoFileAlert: [{ required: true, message: 'MES上机文件不存在时提示不能为空', trigger: 'blur' }],
  isSeparateOrder: [{ required: true, message: '是否分单不能为空', trigger: 'blur' }],
  defaultUnitPrice: [{ required: true, message: '默认基础单价不能为空', trigger: 'blur' }],
  uploadFile: [{ required: true, message: '上传文件不能为空', trigger: 'blur' }],
  separateOrderScheduing: [{ required: true, message: '是否按分单排产不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('baseProcessNameRemark')
const baseProcessNameRemarkFormRef = ref()

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BaseProcessNameApi.getBaseProcessName(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
    await baseProcessNameRemarkFormRef.value.validate()
  } catch (e) {
    subTabsName.value = 'baseProcessNameRemark'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as BaseProcessNameVO
    // 拼接子表的数据
    data.baseProcessNameRemarks = baseProcessNameRemarkFormRef.value.getData()
    if (formType.value === 'create') {
      await BaseProcessNameApi.createBaseProcessName(data)
      message.success(t('common.createSuccess'))
    } else {
      await BaseProcessNameApi.updateBaseProcessName(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    processNumber: undefined,
    processName: undefined,
    processUnit: undefined,
    productionType: undefined,
    remark: undefined,
    isOutsource: undefined,
    outsourceMaterialSpec: undefined,
    productionQuantityCalculateType: undefined,
    defaultMaterialNo: undefined,
    subject: undefined,
    mesManualComplete: undefined,
    semiProductStorage: undefined,
    loadDesignDocuments: undefined,
    processType: undefined,
    status: undefined,
    mesNoFileAlert: undefined,
    isSeparateOrder: undefined,
    defaultUnitPrice: undefined,
    uploadFile: undefined,
    separateOrderScheduing: undefined
  }
  formRef.value?.resetFields()
}
</script>
